package com.mineoj.controller;

import com.mineoj.common.BaseResponse;
import com.mineoj.common.ErrorCode;
import com.mineoj.common.ResultUtils;
import com.mineoj.exception.BusinessException;
import com.mineoj.judge.JudgeService;
import com.mineoj.model.dto.competitionscore.CompetitionScoreAddRequest;
import com.mineoj.model.entity.User;
import com.mineoj.service.CompetitionScoreService;
import com.mineoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 题目接口
 *
 */
@RestController
@RequestMapping("/competition_score")
@Slf4j
public class CompetitionScoreController {

    @Resource
    private CompetitionScoreService competitionScoreService;

    @Resource
    private UserService userService;


    /**
     * 提交题目
     *
     * @param competitionScoreAddRequest
     * @param request
     * @return 提交记录的 id
     */
    @PostMapping("/")
    public BaseResponse<Long> doCompetitionScore(@RequestBody CompetitionScoreAddRequest competitionScoreAddRequest,
                                               HttpServletRequest request) {
        if (competitionScoreAddRequest == null || competitionScoreAddRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能提交题目
        final User loginUser = userService.getLoginUser(request);
        long competitionScoreId = competitionScoreService.doCompetitionQuestionSubmit(competitionScoreAddRequest, loginUser);
        return ResultUtils.success(competitionScoreId);
    }
}
